<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="Vue生命周期先来一个直译的形式 123456789101112131415161718 创建一个Vue实例 new Vue 初始化事件和生命周期 调用 beforeCreate钩子 初始化注入和反应 调用 created 钩子 编译模板阶段	Has el option 这里是判断是否使用了 el这个属性。如果没有使用那么就是属于未挂载的状态，只有在之后 $mount 被调用时才会进行下一步的编译">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue生命周期">
<meta property="og:url" content="http://tallgy.gitee.io/2021/11/16/Vue/Vue%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="Vue生命周期先来一个直译的形式 123456789101112131415161718 创建一个Vue实例 new Vue 初始化事件和生命周期 调用 beforeCreate钩子 初始化注入和反应 调用 created 钩子 编译模板阶段	Has el option 这里是判断是否使用了 el这个属性。如果没有使用那么就是属于未挂载的状态，只有在之后 $mount 被调用时才会进行下一步的编译">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tallgy.gitee.io/.io//lifecycle.png">
<meta property="og:image" content="http://tallgy.gitee.io/.io//lifecycle9.jpg">
<meta property="article:published_time" content="2021-11-16T14:15:21.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.354Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="生命周期">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tallgy.gitee.io/.io//lifecycle.png">


<link rel="canonical" href="http://tallgy.gitee.io/2021/11/16/Vue/Vue%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/11/16/Vue/Vue%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/","path":"2021/11/16/Vue/Vue生命周期/","title":"Vue生命周期"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Vue生命周期 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Vue%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number">1.</span> <span class="nav-text">Vue生命周期</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%86%E6%9E%90%E6%AF%8F%E4%B8%80%E9%98%B6%E6%AE%B5"><span class="nav-number">2.</span> <span class="nav-text">分析每一阶段</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#beforeCreate"><span class="nav-number">2.1.</span> <span class="nav-text">beforeCreate</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#created"><span class="nav-number">2.2.</span> <span class="nav-text">created</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#beforeMount"><span class="nav-number">2.3.</span> <span class="nav-text">beforeMount</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#mounted"><span class="nav-number">2.4.</span> <span class="nav-text">mounted</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#beforeUpdate"><span class="nav-number">2.5.</span> <span class="nav-text">beforeUpdate</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#updated"><span class="nav-number">2.6.</span> <span class="nav-text">updated</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#beforeDestroy"><span class="nav-number">2.7.</span> <span class="nav-text">beforeDestroy</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#destroyed"><span class="nav-number">2.8.</span> <span class="nav-text">destroyed</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/11/16/Vue/Vue%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue生命周期
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-11-16 22:15:21" itemprop="dateCreated datePublished" datetime="2021-11-16T22:15:21+08:00">2021-11-16</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/%E9%9A%8F%E7%AC%94/" itemprop="url" rel="index"><span itemprop="name">随笔</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Vue生命周期"><a href="#Vue生命周期" class="headerlink" title="Vue生命周期"></a>Vue生命周期</h1><p>先来一个直译的形式</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"> 创建一个Vue实例 new Vue</span><br><span class="line"> 初始化事件和生命周期</span><br><span class="line"> 调用 beforeCreate钩子</span><br><span class="line"> 初始化注入和反应</span><br><span class="line"> 调用 created 钩子</span><br><span class="line"> 编译模板阶段</span><br><span class="line">	Has el option 这里是判断是否使用了 el这个属性。如果没有使用那么就是属于未挂载的状态，只有在之后 $mount 被调用时才会进行下一步的编译。</span><br><span class="line">	Has template option，判断是否使用了template参数。如果没有使用template参数，那么就将用el元素时的内部标签，如果使用了template元素，那么就会将template的字符串作为标签覆盖el元素内部的，除非有插槽。</span><br><span class="line">	这里的 outerHTML，我们也知道就是：除了包含innerHTML的全部内容外, 还包含对象标签本身。</span><br><span class="line">然后就是调用 beforeMount 钩子</span><br><span class="line">然后就是创建 虚拟DOM 并且替换 el，这里就是实现了将页面的显示。此时这里在显示的时候就已经创建好了虚拟DOM并且还进行了数据和视图的绑定。</span><br><span class="line">调用 mounted 钩子，但是记住，这里这个钩子并不会保正所有的子组件都被挂载完成，虽然大部分情况都是的。当然可以使用 nexttick 这个函数来保证了所有的组件被渲染。</span><br><span class="line">现在已经装载完成，处于组件在视图中的情况。此时当数据发生改变会引起 beforeupdate钩子</span><br><span class="line">beforeUpdate钩子函数，在数据发生改变的时候，进行虚拟dom的渲染和patch之前。</span><br><span class="line">updated钩子函数，在渲染和patch之后。但是不会保证所有的子组件都刷新。</span><br><span class="line">当调用了 destroy方法，触发了 beforeDestroy钩子</span><br><span class="line">拆卸 watchers，子组件和事件监听</span><br><span class="line">已经被销毁了，调用destroyed</span><br></pre></td></tr></table></figure>

<img src="/.io//lifecycle.png" alt="lifecycle" style="zoom:67%;">

<p>简单就是分为了三大阶段。</p>
<p><strong>初始化，数据响应式，销毁。</strong></p>
<img src="/.io//lifecycle9.jpg" alt="lifecycle9" style="zoom:67%;">



<h1 id="分析每一阶段"><a href="#分析每一阶段" class="headerlink" title="分析每一阶段"></a>分析每一阶段</h1><h2 id="beforeCreate"><a href="#beforeCreate" class="headerlink" title="beforeCreate"></a>beforeCreate</h2><p>​        实例初始化之后，进行数据监听和事件/侦听器的配置之前</p>
<p>​        此时实例已经初始化了，就是有了this的指向，但是还没有开启数据监听和事件的配置。</p>
<p>​        但是没有data属性</p>
<h2 id="created"><a href="#created" class="headerlink" title="created"></a>created</h2><p>​        此时代表已经创建完成，已经将data对象创建好了，那么也代表了已经创建了响应的准备。</p>
<p>​        但是还没有开始编译模板。</p>
<h2 id="beforeMount"><a href="#beforeMount" class="headerlink" title="beforeMount"></a>beforeMount</h2><p>​        此时是创建了虚拟DOM进行了存储，对于存在了el参数的将会即时编译，不存在el参数的将会等待被调用时(vm.$mount(el))，再进行编译。</p>
<p>​        在编译完成之后便会调用beforeMount，此时就是处于编译完成，但是却没有进行页面渲染。</p>
<p>​        在挂载开始之前被调用：相关的 <code>render</code> 函数首次被调用。</p>
<p>​        此时我们可以发现，虽然我们使用 this.$el 会有输出，但是里面的 括号表达式还没有被转换为data的数据。</p>
<p>​        就是属于创建了一个虚拟的节点但是还没有进行处理。</p>
<h2 id="mounted"><a href="#mounted" class="headerlink" title="mounted"></a>mounted</h2><p>​        el 被新创建的 $el 替换了。此时模板已经渲染到了浏览器，</p>
<p>​        但是注意并不会保证所有的子组件也已经被挂载完成。</p>
<h2 id="beforeUpdate"><a href="#beforeUpdate" class="headerlink" title="beforeUpdate"></a>beforeUpdate</h2><p>​        此时是数据已经发生了改变，但是还没有更新DOM时被调用。</p>
<p>​        这里我们可以了解以下Vue的一个更新形式，在被修改了DOM时，是先进行虚拟DOM的修改，然后直到一个执行的结束，然后再进行渲染。这里的一个是指的是一个任务的执行结束加上微任务的结束。而宏任务会被记录为下一个nexttick</p>
<p>​        我们也可以简单的使用代码进行测试</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">new Promise(((resolve, reject) =&gt; &#123;</span><br><span class="line">  this.a = 10;</span><br><span class="line">  resolve(20);</span><br><span class="line">&#125;)).then(res =&gt; &#123;</span><br><span class="line">  this.a = res;</span><br><span class="line">&#125;)</span><br><span class="line">setTimeout(() =&gt; &#123;</span><br><span class="line">  this.a = 30;</span><br><span class="line">&#125;, 0);</span><br><span class="line"></span><br><span class="line">先使用promise，promise内部是一个同步，直到一个异步的开始，便下一个。</span><br><span class="line">then方法会被加入微任务</span><br><span class="line">settimeout方法会被加入宏任务。</span><br><span class="line">所以最后使用</span><br><span class="line">beforeUpdate() &#123;</span><br><span class="line">	// console.log(this.a);</span><br><span class="line">  this.$nextTick(() =&gt; &#123;</span><br><span class="line">    console.log(this.a);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line">输出时，nexttick会等待一个任务加上微任务的结束为标识进行执行。所以先直接输出两次 20</span><br><span class="line">然后再输出一次 30</span><br><span class="line">这里为什么会输出两次20，原因是beforeUpdate执行了两次，所以就运行了两次。</span><br></pre></td></tr></table></figure>



<h2 id="updated"><a href="#updated" class="headerlink" title="updated"></a>updated</h2><p>​        此时，组件的DOM已经更新。可以使用一些依赖DOM的操作，但是不会保证子组件的渲染完成。</p>
<h2 id="beforeDestroy"><a href="#beforeDestroy" class="headerlink" title="beforeDestroy"></a>beforeDestroy</h2><p>​        在实例被销毁之前被调用，此时所有的实例任然完全可以使用。</p>
<h2 id="destroyed"><a href="#destroyed" class="headerlink" title="destroyed"></a>destroyed</h2><p>​        实例销毁之后调用，此时所有的实例已经被解绑，监听已经被移除。所有的子实例也被销毁。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"># Vue</a>
              <a href="/tags/%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/" rel="tag"># 生命周期</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/11/14/JavaScript/%E9%9A%8F%E7%AC%94/%E8%BF%AD%E4%BB%A3%E5%99%A8/" rel="prev" title="迭代器">
                  <i class="fa fa-chevron-left"></i> 迭代器
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/11/17/%E6%B5%8F%E8%A7%88%E5%99%A8/%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%9B%9E%E6%B5%81%E5%92%8C%E9%87%8D%E7%BB%98/" rel="next" title="浏览器的回流和重绘">
                  浏览器的回流和重绘 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
